package com.qyer.search.utils;

import com.google.common.collect.Lists;
import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.model.lastminute.VSKU;

import java.util.*;
/**
 * Created by tangzheng on 16/7/21.
 */
public class BlenderUtils {
  public static List<Integer> blend(Map<Integer, Integer> groupOrder, int step, Map<Integer, List<Integer>> sourceList){

    List<Integer> blendList = new ArrayList<>();
    boolean isEmptySource = true;
    Map<Integer, Integer> sizeMap = new HashMap<>();
    Map<Integer, Integer> indexMap = new HashMap<>();
    Iterator<Map.Entry<Integer, List<Integer>>> it = sourceList.entrySet().iterator();
    while(it.hasNext()){
      Map.Entry<Integer, List<Integer>> entry = it.next();
      int s = entry.getValue().size();
      sizeMap.put(entry.getKey(), s);
      indexMap.put(entry.getKey(), 0);
      if(s > 0)
        isEmptySource = false;
    }

    //System.out.println("isEmptySource");

    if(isEmptySource){
      return  blendList;
    }
    //记录下标

    Set<Integer> isBlend = new HashSet<>();

    int size = groupOrder.size();
    List<Integer> sortList = new ArrayList<>(size);
    List<Integer> sortListTemp = new ArrayList<>(size);
    for(int i = 0; i < size; i++){
      sortList.add(-1);
    }
    for(Map.Entry<Integer, Integer> entry:groupOrder.entrySet()){
      sortList.add((entry.getValue() - 1), entry.getKey());
      sortListTemp.add(entry.getKey());
    }
    //无顺序，随机指定
    if(sortList.get(size - 1) == -1){
      sortList = sortListTemp;
    }
    //混排
    boolean isComplete = false;
    int ss = sortList.size();
    while(!isComplete) {
      isComplete = true;
      for (int i = 0; i < ss; i++){
        //System.out.println("is");
        int stg = sortList.get(i);
        if(sourceList.containsKey(stg)) {
          int index = indexMap.get(stg);
          int sssss = sizeMap.get(stg);
          List<Integer> list = sourceList.get(stg);
          if (index < sssss) {
            isComplete = false;
            int index2 = index;
            for (int j = index; ((j < sssss) && j < (index + step)); j++) {
              index2++;
              indexMap.put(stg, index2);
              //System.out.println("as");
              int id = list.get(j);
              if (!isBlend.contains(id)) {
                isBlend.add(id);
                blendList.add(id);
              }
            }
          }
        }
      }
    }
    return blendList;
  }


  public static List<VSKU> blend(Map<Integer, Integer> groupOrder, Map<Integer, List<VSKU>> sourceList, int step){

    //System.out.println("STG_Order:"+groupOrder);
    List<VSKU> blendList;
    boolean isEmptySource = true;
    Map<Integer, Integer> sizeMap = new HashMap<>();
    Map<Integer, Integer> indexMap = new HashMap<>();
    Iterator<Map.Entry<Integer, List<VSKU>>> it = sourceList.entrySet().iterator();
    int totalSize = 0;
    while(it.hasNext()){
      Map.Entry<Integer, List<VSKU>> entry = it.next();
      int s = entry.getValue().size();
      List<VSKU> tmp = entry.getValue();
      //////////////////////////
      totalSize = totalSize + s;
      sizeMap.put(entry.getKey(), s);
      indexMap.put(entry.getKey(), 0);
      if(s > 0)
        isEmptySource = false;
    }

    //System.out.println("isEmptySource");

    if(isEmptySource){
      return  new ArrayList<>();
    }
    blendList = Lists.newArrayListWithCapacity(totalSize);
    //记录下标

    Set<Integer> isBlend = new HashSet<>();

    int size = groupOrder.size();
    List<Integer> sortList = new ArrayList<>(size);
    List<Integer> sortListTemp = new ArrayList<>(size);
    for(int i = 0; i < size; i++){
      sortList.add(-1);
    }
    for(Map.Entry<Integer, Integer> entry:groupOrder.entrySet()){
      try {
        sortList.add((entry.getValue() - 1), entry.getKey());
      }catch (Exception e){
        //sortList.add(entry.getValue(), entry.getKey());
        //System.out.println("Exception~"+"KEY:"+entry.getKey()+"~Fond:"+entry.getValue());
      }
      sortListTemp.add(entry.getKey());
    }
    //无顺序，随机指定
    //System.out.println("顺序:"+sortList);
    if(sortList.get(size - 1) == -1){
      sortList = sortListTemp;
      /*System.out.println("随机STG Order:");
      for(int i = 0; i < sortList.size(); i++){
        System.out.println("SE:"+i+"   STG:"+sortList.get(i));
      }*/
    }
    //混排
    boolean isComplete = false;
    int ss = sortList.size();
    while(!isComplete) {
      isComplete = true;
      for (int i = 0; i < ss; i++){
        //System.out.println("is");
        int stg = sortList.get(i);
        if(sourceList.containsKey(stg)) {
          int index = indexMap.get(stg);
          int sssss = sizeMap.get(stg);
          List<VSKU> list = sourceList.get(stg);
          if (index < sssss) {
            isComplete = false;
            int index2 = index;
            for (int j = index; ((j < sssss) && j < (index + step)); j++) {
              index2++;
              indexMap.put(stg, index2);
              //System.out.println("as");
              VSKU vsku = list.get(j);
              int id = vsku.getId();
              if (!isBlend.contains(id)) {
                isBlend.add(id);
                blendList.add(vsku);
              }
            }
          }
        }
      }
    }
    return blendList;
  }
}
